#
# project data
#
PROJECT_NAME=avdl
PROJECT_VERSION=0.0.1

#
# compiler data
#
#COMPILER_FLAGS=-Wall -Werror -Wpedantic
COMPILER_FLAGS=-Wall -Wpedantic
COMPILER_DEFINES=-DINSTALL_LOCATION=\"${INSTALL_LOCATION}/usr\" \
	-DPROJECT_NAME=\"${PROJECT_NAME}\"
COMPILER_INCLUDES=-Iinclude -I${DIRECTORY_OBJ}

#
# directories
#
DIRECTORY_BUILD=build
DIRECTORY_EXE=${DIRECTORY_BUILD}/bin
DIRECTORY_OBJ=${DIRECTORY_BUILD}/objects
DIRECTORY_ALL=${DIRECTORY_BUILD} ${DIRECTORY_EXE} ${DIRECTORY_OBJ}

#
# source files
#
SRC=$(wildcard src/*.c)
OBJ=${SRC:src/%.c=${DIRECTORY_OBJ}/%.o}

HEADERS=$(widcard include/*.h)

#
# executable
#
EXECUTABLE=${DIRECTORY_EXE}/${PROJECT_NAME}

#
# engine data
#
ENGINE_PATH=engines/cengine
ENGINE_OUT=${ENGINE_PATH}/build/libavdl-cengine.a


INSTALL_LOCATION=@INSTALL_LOCATION@

# android
DIRECTORIES=engines/android/app/src/main/cpp/engine/
CENGINE_FILES_HEADER=$(wildcard engines/cengine/include/*.h)
CENGINE_FILES_SRC=$(wildcard engines/cengine/src/*.c)
CENGINE_FILES_ANDROID_HEADER=$(CENGINE_FILES_HEADER:engines/cengine/include/%.h=engines/android/app/src/main/cpp/engine/%.h)
CENGINE_FILES_ANDROID_SRC=$(CENGINE_FILES_SRC:engines/cengine/src/%.c=engines/android/app/src/main/cpp/engine/%.c)
CENGINE_FILES_LOCAL=$(CENGINE_FILES_SRC:engines/cengine/src/%.c=engine\/%.c)

all: ${EXECUTABLE} ${ENGINE_OUT}

# build the executable, depends on source, lex, yacc and all engines
${EXECUTABLE}: ${DIRECTORY_ALL} ${DIRECTORIES} ${OBJ} ${CENGINE_FILES_ANDROID_HEADER} ${CENGINE_FILES_ANDROID_SRC} engines/android/app/src/main/cpp/CMakeLists.txt
	$(CC) ${COMPILER_FLAGS} ${COMPILER_DEFINES} ${COMPILER_INCLUDES} ${OBJ} -o $@

# how to build the c engine
engine: ${ENGINE_OUT}

${ENGINE_OUT}:
	${MAKE} -C ${ENGINE_PATH}

install: ${EXECUTABLE} ${ENGINE_OUT}
	${MAKE} -C ${ENGINE_PATH} PREFIX=${INSTALL_LOCATION} install
	mkdir -p ${INSTALL_LOCATION}/usr/bin
	install ${EXECUTABLE} ${INSTALL_LOCATION}/usr/bin/
	mkdir -p ${INSTALL_LOCATION}/usr/share/man/man1/
	install manual/avdl.1 ${INSTALL_LOCATION}/usr/share/man/man1/
	mkdir -p ${INSTALL_LOCATION}/usr/share/avdl/android
	cp -r engines/android/* ${INSTALL_LOCATION}/usr/share/avdl/android

#mkdir -p ${INSTALL_LOCATION}/share/info/
#install avdl.info.gz ${INSTALL_LOCATION}/share/info/
# vim files
#mkdir -p ${INSTALL_LOCATION}/share/vim/vimfiles/syntax/
#install vim/syntax/avdl.vim ${INSTALL_LOCATION}/share/vim/vimfiles/syntax/
#mkdir -p ${INSTALL_LOCATION}/share/vim/vimfiles/ftdetect/
#install vim/ftdetect/avdl.vim ${INSTALL_LOCATION}/share/vim/vimfiles/ftdetect/

tarball: ${PROJECT_NAME}.tar

${PROJECT_NAME}.tar:
	tar cf $@ src makefile engines include

clean:
	rm -f ${EXECUTABLE}

destclean: clean
	rm -f avdl.info.gz makefile

# android files
engines/android/app/src/main/cpp/engine/%.h: engines/cengine/include/%.h
	cp $< $@

engines/android/app/src/main/cpp/engine/%.c: engines/cengine/src/%.c
	cp $< $@

engines/android/app/src/main/cpp/CMakeLists.txt: engines/android/app/src/main/cpp/CMakeLists.txt.in
	sed 's/%AVDL_ENGINE_FILES%/${CENGINE_FILES_LOCAL}/' $< > $@

${DIRECTORIES} ${DIRECTORY_ALL}:
	mkdir -p $@

${DIRECTORY_OBJ}/%.o: src/%.c ${HEADERS}
	$(CC) ${COMPILER_FLAGS} ${COMPILER_DEFINES} ${COMPILER_INCLUDES} -c $< -o $@


.PHONY: clean destclean install engine
